package brinaryTree;

public class O33_VerifyPostorder {
    public boolean verifyPostorder(int[] postorder) {
        return recursion(postorder, 0, postorder.length - 1);
    }

    private boolean recursion(int[] postorder, int i, int j) {
        if (i >= j) return true;
        int p = i;
        while (postorder[p] < postorder[j]) p++;
        int m = p;
        while (postorder[p] > postorder[j]) p++;
        return p == j && recursion(postorder, i, m - 1) && recursion(postorder, m, j - 1);
    }

    public static void main(String[] args) {
        O33_VerifyPostorder solution = new O33_VerifyPostorder();
        int[] postorder = {1, 3, 2, 6, 5};
        System.out.println(solution.verifyPostorder(postorder));
    }
}
